/*=========================================================================

  Library:   HMP

  Copyright (c) Kitware Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=========================================================================*/

// Qt includes

// HMP includes
#include "BooleanMapper.h"

//-----------------------------------------------------------------------------
class BooleanMapperPrivate
{
public:
  BooleanMapperPrivate();
  QByteArray PropertyName;
  QVariant TrueValue;
  QVariant FalseValue;
};

// --------------------------------------------------------------------------
BooleanMapperPrivate::BooleanMapperPrivate()
{
  this->TrueValue = QVariant(true);
  this->FalseValue = QVariant(false);
}

// --------------------------------------------------------------------------
// BooleanMapper methods

// --------------------------------------------------------------------------
BooleanMapper::BooleanMapper(
  QObject* targetObject, const QByteArray& property, const QByteArray& signal)
  : QObject(targetObject)
  , d_ptr(new BooleanMapperPrivate)
{
  Q_ASSERT(property.isEmpty() != true);
  Q_ASSERT(targetObject != 0);
  Q_D(BooleanMapper);
  d->PropertyName = property;
  if (!signal.isEmpty())
    {
    connect(targetObject, signal, this, SLOT(emitValueChanged()));
    }
}

// --------------------------------------------------------------------------
BooleanMapper::~BooleanMapper()
{
}

// --------------------------------------------------------------------------
QByteArray BooleanMapper::propertyName()const
{
  Q_D(const BooleanMapper);
  return d->PropertyName;
}

// --------------------------------------------------------------------------
QObject* BooleanMapper::targetObject()const
{
  return this->parent();
}

// --------------------------------------------------------------------------
QVariant BooleanMapper::trueValue()const
{
  Q_D(const BooleanMapper);
  return d->TrueValue;
}

// --------------------------------------------------------------------------
QVariant BooleanMapper::falseValue()const
{
  Q_D(const BooleanMapper);
  return d->FalseValue;
}

// --------------------------------------------------------------------------
void BooleanMapper::setTrueValue(const QVariant& trueValue)
{
  Q_D(BooleanMapper);
  if (d->TrueValue == trueValue)
    {
    return;
    }
  d->TrueValue = trueValue;
  this->emitValueAsChanged();
}

// --------------------------------------------------------------------------
void BooleanMapper::setFalseValue(const QVariant& falseValue)
{
  Q_D(BooleanMapper);
  if (d->FalseValue == falseValue)
    {
    return;
    }
  d->FalseValue = falseValue;
  this->emitValueAsChanged();
}

// --------------------------------------------------------------------------
bool BooleanMapper::value()const
{
  return this->targetObject()->property(this->propertyName()).toBool();
}

// --------------------------------------------------------------------------
bool BooleanMapper::complement()const
{
  return !this->value();
}

// --------------------------------------------------------------------------
int BooleanMapper::valueAsInt()const
{
  Q_D(const BooleanMapper);
  return this->value() ? d->TrueValue.toInt() : d->FalseValue.toInt();
}

// --------------------------------------------------------------------------
QString BooleanMapper::valueAsString()const
{
  Q_D(const BooleanMapper);
  return this->value() ? d->TrueValue.toString() : d->FalseValue.toString();
}

// --------------------------------------------------------------------------
void BooleanMapper::setValue(bool value)
{
  this->targetObject()->setProperty(this->propertyName(), QVariant(value));
}

// --------------------------------------------------------------------------
void BooleanMapper::setComplement(bool value)
{
  this->setValue(!value);
}

// --------------------------------------------------------------------------
void BooleanMapper::setValueAsInt(int intValue)
{
  Q_D(BooleanMapper);
  this->setValue( QVariant(intValue) == d->TrueValue);
}

// --------------------------------------------------------------------------
void BooleanMapper::setValueAsString(const QString& stringValue)
{
  Q_D(BooleanMapper);
  this->setValue( QVariant(stringValue) == d->TrueValue );
}

// --------------------------------------------------------------------------
void BooleanMapper::toggle()
{
  this->setValue(this->complement());
}

// --------------------------------------------------------------------------
void BooleanMapper::emitValueChanged()
{
  emit valueChanged(this->value());
  emit complementChanged(this->complement());
  this->emitValueAsChanged();
}

// --------------------------------------------------------------------------
void BooleanMapper::emitValueAsChanged()
{
  emit valueAsIntChanged(this->valueAsInt());
  emit valueAsStringChanged(this->valueAsString());
}
